class map {
    gravity;
    tilt;
    tilt_sensitivity;
    radiantilt;
    tilt_max;
    vel;
    friction;
    pidivide180;


    constructor() {
        this.init();
    }

    init() {
        this.gravity = { x: 0, y: 1 };

        this.tilt = { now: 0, last: 0 };
        this.tilt_sensitivity = 0.01;
        this.radiantilt = 0;

        this.tilt_max = 0.4, //0.5 is 90 degrees

        this.vel = { x: 0, y: 0, sensitivity: 0.5 };
        this.friction = 0.6;

        this.pidivide180 = (180 / Math.PI);
    }

    update() {
        this.tilt.last = this.tilt.now;

        this.vel.x *= this.friction;
        this.vel.y *= this.friction;

        if (Math.abs(this.vel.x) < 0.0001) {
            this.vel.x = 0;
        }

        if (window.game.system.inputmanager.keys_down.RIGHTARROW ||
            window.game.system.inputmanager.checkinput('RIGHT_BUTTON')) {
            this.tilt.now += this.tilt_sensitivity;
            if (this.tilt.now > this.tilt_max) {
                this.tilt.now = this.tilt_max;
            }
            else {
                //if not at far edge, add velocity
                this.vel.x += this.vel.sensitivity;
            }
        }
        if (window.game.system.inputmanager.keys_down.LEFTARROW ||
            window.game.system.inputmanager.checkinput('LEFT_BUTTON')) {
            this.tilt.now -= this.tilt_sensitivity;
            if (this.tilt.now < this.tilt_max * -1) {
                this.tilt.now = this.tilt_max * -1;
            }
            else {
                //if not at far edge, add velocity
                this.vel.x -= this.vel.sensitivity;
            }
        }

        this.gravity.x = this.tilt.now * Math.PI;
        this.gravity.y = Math.cos((this.tilt.now * Math.PI));

        this.gravity = this.normalizevector(this.gravity.x, this.gravity.y);
    }

    draw() {
    }


    normalizevector(x, y) {
        if (x === 0 && y === 0) //check for degenerate case
        { return { x: 0, y: 0 }; }

        //calculate length
        var length = Math.sqrt((x * x) + (y * y));
        //divide components by length
        return { x: x / length, y: y / length };
    }

    vectorlength(vector) {

        return Math.sqrt((vector.x * vector.x) + (vector.y * vector.y));
    }
} 